package com.valuegroup.smarttodo.ui.programme;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.ui.programme.curriculum.CurriculumFragment;
import com.valuegroup.smarttodo.ui.programme.goals.GoalsFragment;
import com.valuegroup.smarttodo.ui.programme.ideality.IdealityFragment;
import com.valuegroup.smarttodo.ui.programme.weekplan.WeekPlanFragment;
import com.valuegroup.smarttodo.plugins.indicator.Indicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 规划-首页
 */
public class ProgrammeFragment extends Fragment {
    private static final String TAG = ProgrammeFragment.class.getSimpleName();

    private Unbinder unbinder;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.indicator)
    Indicator indicator;

    public ProgrammeFragment() {
    }

    public static ProgrammeFragment newInstance() {
        return new ProgrammeFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_programme, container, false);
        unbinder = ButterKnife.bind(this, view);


        // 初始化工具条
        AppCompatActivity aty = ((AppCompatActivity) getActivity());
        aty.setSupportActionBar(toolbar);
        // 隐藏Toolbar的标题栏
        aty.getSupportActionBar().setDisplayShowTitleEnabled(false);
        // 需要在fragment中配置actionbar可先的属性,如果不设置这个属性，fragment中的onCreateOptionsMenu()回调不会被执行！
        setHasOptionsMenu(true);

        viewPager.setAdapter(new ProgrammePagerAdapter(getChildFragmentManager()));
        indicator.setViewPager(viewPager);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private class ProgrammePagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments = new ArrayList<>();

        public ProgrammePagerAdapter(FragmentManager fm) {
            super(fm);
            fragments.add(WeekPlanFragment.newInstance());
            fragments.add(CurriculumFragment.newInstance());
            fragments.add(GoalsFragment.newInstance());
            fragments.add(IdealityFragment.newInstance());
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}
